package model.pojo;

import java.io.Serializable;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import models.util.MySqlDataAccessHelper;
public class Employee implements Serializable {

	private static final long serialVersionUID = 1L;
	private String idemployee;
	private String userName;
	private String password;
	private String repassword;
	private String fullName;
	private String phone;
	private Date birthday;
	private String address;
	private String email;
	private String sex;
	private String position;
	private String idmanager;
	private Integer iddepartment;
	private Integer idrole;
	private String description;
	private Department department;
	private Roles role;
	
	private String manager;
	private String sexname;
	private String departmentName;
	private String ngaysinh;
	private String urlBirthday="resources/css/Images/imgLien/dotBirthday.gif";
	private int accountant;
	private boolean checked;	
	
	private Date startWork;
	private String startDate;
	private String cmnd;
	private String bankAccount;
	private String employeeCode;
	private int state;
	private String bankName;
	private String mobile;
	private String endDate;
	private Date endWork;
	private String image;
	
	private String workingDay;
	private String db=null;
		
	public String getDb() {
		return db;
	}

	public void setDb(String db) {
		this.db = db;
	}
	public Employee(String fullname){
		this.fullName=fullname;
	}

	public Employee(String id,String fullname,String us){
		this.idemployee=id;
		this.fullName=fullname;
		this.userName=us;
	}
	
	public Employee(String idemployee, String userName,String password,String fullName, String phone, Date birthday,String address, 
			String email, String sex, String position,Integer iddepartment, Integer idrole,int accoutant,
			String departmentName,Date startWork, String cmnd, String bankAccount,String bankName,
			String employeeCode, int state, String mobile, Date endWork, String image,String description){
		this.idemployee = idemployee;
		this.userName = userName;
		this.password = password;
		this.fullName = fullName;
		this.phone = phone;
		this.birthday = birthday;
		this.address = address;
		this.email = email;
		this.sex = sex;
		this.position = position;
		this.iddepartment = iddepartment;
		this.idrole = idrole;
		this.accountant = accoutant;
		this.departmentName = departmentName;
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		this.ngaysinh = format.format(birthday);
		this.startWork = startWork;
		this.startDate = format.format(startWork);
		this.cmnd = cmnd;
		this.bankAccount = bankAccount;
		this.employeeCode = employeeCode;
		this.state = state;
		this.bankName = bankName;
		this.mobile = mobile;
		this.endWork = endWork;
		this.endDate = format.format(endWork);
		this.image = image;
		this.description = description;
	}
	
	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public int getAccountant() {
		return accountant;
	}

	public void setAccountant(int accountant) {
		this.accountant = accountant;
	}

	private String style="width:1px;height:1px;";
	
	
	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}
			
	public String getUrlBirthday() {
		return urlBirthday;
	}

	public void setUrlBirthday(String urlBirthday) {
		this.urlBirthday = urlBirthday;
	}

	public String getNgaysinh() {
		return ngaysinh;
	}

	public void setNgaysinh(String ngaysinh) {
		this.ngaysinh = ngaysinh;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public String getManager() {
		return manager;
	}

	public void setManager(String manager) {
		this.manager = manager;
	}

	public String getSexname() {
		return sexname;
	}

	public void setSexname(String sexname) {
		this.sexname = sexname;
	}

	

	

	public Employee() {
		//DateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
		this.idemployee = "";
		this.userName = "";
		this.repassword="";
		this.password = "";
		this.fullName = "";
		this.phone = "";

		this.birthday = new Date();
		this.startWork = new Date();
		this.address = "";
		this.email = "";
		this.sex = "1";
		this.position = "";
		this.idmanager = "";
		this.iddepartment = 1;
		this.idrole = 2;
		this.description = "";
		this.urlBirthday="/resources/css/Images/imgLien/dotBirthday.gif";
		this.style="width:1px;height:1px;";
	}

	public Employee(String userName, String password) {
		this.userName = userName;
		this.password = password;
	}

	public Employee(String idemployee, String userName, String password,
			String fullName, String phone, Date birthday, String address,
			String email, String sex, String position, String idmanager,
			Integer iddepartment, Integer idrole, String description) {
		this.idemployee = idemployee;
		this.userName = userName;
		this.password = password;
		this.fullName = fullName;
		this.phone = phone;
		this.birthday = birthday;
		this.address = address;
		this.email = email;
		this.sex = sex;
		this.position = position;
		this.idmanager = idmanager;
		this.iddepartment = iddepartment;
		this.idrole = idrole;
		this.description = description;
	}
	public Employee(String idemployee, String userName,String repassword, String password,
			String fullName, String phone, Date birthday, String address,
			String email, String sex, String position, String idmanager,
			Integer iddepartment, Integer idrole, String description,int accountant) {
		this.idemployee = idemployee;
		this.userName = userName;
		this.repassword=repassword;
		this.password = password;
		this.fullName = fullName;
		this.phone = phone;
		this.birthday = birthday;
		this.address = address;
		this.email = email;
		this.sex = sex;
		this.position = position;
		this.idmanager = idmanager;
		this.iddepartment = iddepartment;
		this.idrole = idrole;
		this.description = description;
		this.accountant = accountant;
	}
	public Employee(String idemployee, String userName,String repassword, String password,
			String fullName, String phone, Date birthday, String address,
			String email, String sex, String position, String idmanager,
			Integer iddepartment, Integer idrole, String description) {
		this.idemployee = idemployee;
		this.userName = userName;
		this.repassword=repassword;
		this.password = password;
		this.fullName = fullName;
		this.phone = phone;
		this.birthday = birthday;
		this.address = address;
		this.email = email;
		this.sex = sex;
		this.position = position;
		this.idmanager = idmanager;
		this.iddepartment = iddepartment;
		this.idrole = idrole;
		this.description = description;
	}
	
	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public String getIdemployee() {
		return this.idemployee;
	}

	public void setIdemployee(String idemployee) {
		this.idemployee = idemployee;
	}

	public String getUserName() {
		return this.userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullName() {
		return this.fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getBirthday() {
		return this.birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSex() {
		return this.sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPosition() {
		return this.position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getIdmanager() {
		return idmanager;
	}

	public void setIdmanager(String idmanager) {
		this.idmanager = idmanager;
	}

	public Integer getIddepartment() {
		return this.iddepartment;
	}

	public void setIddepartment(Integer iddepartment) {
		this.iddepartment = iddepartment;
	}

	public Integer getIdrole() {
		return this.idrole;
	}

	public void setIdrole(Integer idrole) {
		this.idrole = idrole;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Department getDepartment(String lang) {
		if(db!=null){
			MySqlDataAccessHelper mysqlHelper = new MySqlDataAccessHelper();
			
			try {
				mysqlHelper.open(db);
				String sql = "select * from department,department_lang where department.IDDepartment=department_lang.IDDepartment and department_lang.LangID='"+lang+"' and department.IDDepartment="
						+ this.iddepartment;
				ResultSet rs = mysqlHelper.executeQuery(sql);
				while (rs.next()) {
					department = new Department();
					department.setIddepartment(rs.getInt("IDDepartment"));
					department.setDepartmentName(rs.getString("DepartmentName"));
					department.setLocation(rs.getString("Location"));
					department.setDescription(rs.getString("Description"));
//					ConfigStaticLang configStaticLang = department
//							.getConfigStaticLang();
//					if (configStaticLang != null) {
//						department.setiDDepartment(rs.getInt("IDDepartment"));
//						department.setDepartmentName(configStaticLang
//								.getNameByLang());
//						department.setLocation(rs.getString("Location"));
//						department.setDescription(rs.getString("Description"));
//					} else {
//						department.setiDDepartment(rs.getInt("IDDepartment"));
//						department
//								.setDepartmentName(rs.getString("DepartmentName"));
//						department.setLocation(rs.getString("Location"));
//						department.setDescription(rs.getString("Description"));
//					}
				}
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				mysqlHelper.close();
			}
		}
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Roles getRole() {
		if(db!=null){
			MySqlDataAccessHelper mysqlHelper = new MySqlDataAccessHelper();
			
			try {
				mysqlHelper.open(db);
				String sql = "select * from roles where IDRole=" + this.idrole;
				ResultSet rs = mysqlHelper.executeQuery(sql); 
				while (rs.next()) {
					role = new Roles(rs.getInt(1), rs.getBoolean(2),
							rs.getBoolean(3), rs.getBoolean(4), rs.getBoolean(5),
							rs.getBoolean(6), rs.getBoolean(7), rs.getString(8));
				}
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				mysqlHelper.close();
			}
		}
		
		return role;
	}

	public void setRole(Roles role) {
		this.role = role;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setCmnd(String cmnd) {
		this.cmnd = cmnd;
	}

	public String getCmnd() {
		return cmnd;
	}

	public void setBankAccount(String bankAccount) {
		this.bankAccount = bankAccount;
	}

	public String getBankAccount() {
		return bankAccount;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setStartWork(Date startWork) {
		this.startWork = startWork;
	}

	public Date getStartWork() {
		return startWork;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getState() {
		return state;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getBankName() {
		return bankName;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getMobile() {
		return mobile;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndWork(Date endWork) {
		this.endWork = endWork;
	}

	public Date getEndWork() {
		return endWork;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getImage() {
		return image;
	}

	public void setWorkingDay(String workingDay) {
		this.workingDay = workingDay;
	}

	public String getWorkingDay() {
		try{
			processCalculateDate();
		}catch (Exception e) {
			e.printStackTrace(System.out);
			return "unknown";
		}        
		return workingDay;
	}
	
	public void processCalculateDate(){
		try{
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(startWork);
	        Calendar cal2 = Calendar.getInstance();
	        long diffDays = 0;	        
	        if(state == 1){
	        	long milis1 = cal1.getTimeInMillis();
		        long milis2 = cal2.getTimeInMillis();
		        long diff = milis2 - milis1;
		        diffDays = diff / (24 * 60 * 60 * 1000);
		        
	        }else if(state == 0){
	        	cal2.setTime(endWork);
	        	long milis1 = cal1.getTimeInMillis();
		        long milis2 = cal2.getTimeInMillis();
		        long diff = milis2 - milis1;
		        diffDays = diff / (24 * 60 * 60 * 1000);
	        }
	        workingDay = diffDays + "";
		}catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return fullName.toString();
	}
}
